package com.snailvr.tv.ui.activity;

import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.RelativeLayout;

import com.snail.readyvr.R;
import com.snail.readyvr.event.LoginEvent;
import com.snail.readyvr.util.StartActivityUtils;
import com.snail.readyvr.util.ToastUtils;
import com.snailvr.tv.Definition;
import com.snailvr.tv.contract.ChannelContact;
import com.snailvr.tv.network.entity.ChannelInfoResponBean;
import com.snailvr.tv.network.entity.ChannelListResponBean;
import com.snailvr.tv.network.entity.PlayUrlResponBean;
import com.snailvr.tv.presenter.ChannelPresenter;
import com.snailvr.tv.ui.adapter.ChannelAdapter;
import com.snailvr.tv.ui.adapter.GridSpacingItemDecoration;
import com.snailvr.tv.ui.base.CheckPermissionsActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.snailvr.tv.Definition.CHANNEL_ID_NAME;


/**
 * Created by Administrator on 2016/9/30.
 */
public class ChannelListActivity extends CheckPermissionsActivity implements ChannelContact.IChanelView{

    private List<ChannelListResponBean.DataBean.ListBean> mChannelsList;
    private ChannelAdapter mChannelAdapter;
    private ChannelContact.IChanelPresenter mPresenter;
    private int page;
    private int page_size;
    private int channel_id;


    @BindView(R.id.recycler_view_channel_list)
    RecyclerView mRecyclerView;

    @BindView(R.id.buffering_view)
    RelativeLayout mButteringView;

    @Override
    protected void initVariables() {

        page = 0;
        page_size = 10;
        mChannelsList = new ArrayList<>();
        mPresenter = new ChannelPresenter();
        mPresenter.registerView(this);
        channel_id = getIntent().getIntExtra(CHANNEL_ID_NAME, 0);

        mChannelAdapter = new ChannelAdapter(this,mChannelsList,channel_id);
    }

    @Override
    protected void initViews(Bundle savedInstanceState) {

        setContentView(R.layout.channel_list);
        ButterKnife.bind(this);

//      LinearLayoutManager manager = new LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL,false);
        GridLayoutManager manager = new GridLayoutManager(this, Definition.COLUMU_NUM);

        mRecyclerView.setLayoutManager(manager);
        mRecyclerView.setAdapter(mChannelAdapter);
        mRecyclerView.addItemDecoration(new GridSpacingItemDecoration(Definition.COLUMU_NUM,Definition.COLUMU_SPACE));

//        mRecyclerView.addItemDecoration(new VerticalDividerItemDecoration.Builder(this)
//                .colorResId(R.color.color_transparent)
//                .size(2)
//                .build());

    }

    @Override
    protected void loadData() {
        mPresenter.loadChanelList(page_size,page,false,false);
        mButteringView.setVisibility(View.VISIBLE);

    }


    @OnClick(R.id.id_layout_close)
    public void onClose() {
        this.finish();
    }


    @Override
    public void showChanelList(ChannelListResponBean chanels) {
        mButteringView.setVisibility(View.GONE);
        mChannelsList.clear();
        mChannelsList.addAll(chanels.getData().getList());
        mChannelAdapter.notifyDataSetChanged();

    }

    @Override
    public void showLoadFailureMsg(int code, String errorMsg) {
        mButteringView.setVisibility(View.GONE);
        ToastUtils.show(this,errorMsg);
        if (code == 001013) {
            StartActivityUtils.startLoginActivity(this,null, LoginEvent.FROM_PLAYER);
        }


    }

    @Override
    public void showChanelInfo(ChannelInfoResponBean.DataBean chanelInfo) {

    }

    @Override
    public void showPlayings(PlayUrlResponBean _Event) {

    }

    @Override
    protected void hasGetAllPermissions() {

    }

    @Override
    public void finish() {
        super.finish();
        overridePendingTransition(0,R.anim.slide_bottom_out);
        mPresenter.unregisterView();
    }
}
